﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace OneByOneSolitaire
{
    public partial class Settings : PhoneApplicationPage
    {
        private DataBase m_db;
        private List<Player> m_players = new List<Player>();

        public Settings()
        {
            InitializeComponent();
            switch (MainPage.ballImage) {
                case 0:
                    showBox(ball1);
                    break;
                case 1:
                    showBox(ball2);
                    break;
                case 2:
                    showBox(ball3);
                    break;
                case 3:
                    showBox(ball4);
                    break;
                case 4:
                    showBox(ball5);
                    break;
                case 5:
                    showBox(ball6);
                    break;
                case 6:
                    showBox(ball7);
                    break;
                case 7:
                    showBox(ball8);
                    break;
            }
            
            if (MainPage.moveDiagonalAllowed) {
                chkDiagonalMoves.IsChecked = true;
            }

            m_db = new DataBase(DataBase.DBConnectionString);

            foreach (Player player in m_db.players) {
                m_players.Add(player);
            }
            lsPlayers.ItemsSource = m_players;
            lsPlayers.SelectedItem = m_db.getPlayer();
        }

        private void showBox(Image ball) {
            Canvas.SetLeft(box, Canvas.GetLeft(ball) + 5);
            Canvas.SetTop(box, Canvas.GetTop(ball) - 5);
        }

        private void cmdDone_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void ball1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            MainPage.ballImage = 0;
            showBox(sender as Image);
        }

        private void ball2_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            MainPage.ballImage = 1;
            showBox(sender as Image);
        }

        private void ball3_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            MainPage.ballImage = 2;
            showBox(sender as Image);
        }

        private void ball4_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            MainPage.ballImage = 3;
            showBox(sender as Image);
        }

        private void ball5_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            MainPage.ballImage = 4;
            showBox(sender as Image);
        }

        private void ball6_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            MainPage.ballImage = 5;
            showBox(sender as Image);
        }

        private void ball7_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            MainPage.ballImage = 6;
            showBox(sender as Image);
        }

        private void ball8_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            MainPage.ballImage = 7;
            showBox(sender as Image);
        }

        private void chkDiagonalMoves_Checked(object sender, RoutedEventArgs e)
        {
            MainPage.moveDiagonalAllowed = chkDiagonalMoves.IsChecked == true;
        }

        private void lsPlayers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            lsGames.Items.Clear();
            if (lsPlayers.SelectedItem == null)
            {
                txPlayer.Text = "";
            }
            else
            {
                Player player = lsPlayers.SelectedItem as Player;
                txPlayer.Text = player.pl_name;
                List<Game> games = m_db.getGamesForPlayer(player.pl_id);
                foreach (Game game in games)
                {
                    lsGames.Items.Add(game);
                }
            }

        }

        private void cmdNewPlayer_Click(object sender, RoutedEventArgs e)
        {
            lsPlayers.SelectedItem = null;
            txPlayer.Focus();
        }

        private void cmdSave_Click(object sender, RoutedEventArgs e)
        {
            if (lsPlayers.SelectedItem == null)
            {
                Player player = m_db.createPlayer(txPlayer.Text);
                if (player != null)
                {
                    m_players.Add(player);
                    lsPlayers.ItemsSource = null;
                    lsPlayers.ItemsSource = m_players;
                    lsPlayers.SelectedItem = player;
                }
            }
            else 
            {
                Player player = lsPlayers.SelectedItem as Player;
                player.pl_name = txPlayer.Text;
                m_db.SubmitChanges();
                int index = lsPlayers.SelectedIndex;
            }
        }

        private void lsGames_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lsGames.SelectedItem != null)
            {
                Game game = lsGames.SelectedItem as Game;
                MainPage.gameId = game.gm_id;
                MainPage.resetLastMove();
            }
            else
                MainPage.gameId = 0;
        }
    }
}